//
//  JXLVisitorTableViewController.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/9/21.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit

class JXLVisitorTableViewController: UITableViewController, JXLVisitorViewDelegate {

    //定义一个是否登陆的属性,如果从沙盒中取到用户数据，则登录成功
    var isLogin: Bool = JXLUserAccountViewModel.sharedUserAccountViewModel.isLogin
    
    //定义visitView的属性
    var visitorView: JXLVisitorView?
    // MARK: 自定义view视图, 重写loadView是自定义控制器中的视图view
    override func loadView() {
        if isLogin {
            super.loadView()
        } else {
            //自己提供一个视图
            visitorView = JXLVisitorView()
            view = visitorView
            //设置代理
            visitorView?.delegate = self
            //定义闭包，将来push登录界面
            visitorView?.loginCloser = {[weak self] in
            print("我是访客视图调用过来的")
           self?.requestLoginAction()
            }
            //未登陆状态下，添加导航栏登陆与注册按钮
            setupNavUI()
            
        }
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
    }

    //添加导航栏登陆与注册按钮
    private func setupNavUI() -> Void {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "登录", imageName: nil, target: self, action: #selector(loginAction))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "注册", imageName: nil, target: self, action: #selector(loginAction))
    }
   
    //点击方法
 @objc private func loginAction() {
        print("点击登录")
    requestLoginAction()
    }
    
    //MARK: JXLVisitorViewDelegate代理方法＝＝＝＝＝
    func didSelectedLogin() {
        print("我是访客视图代理对象调用过来的")
    }
    
    private func requestLoginAction() {
        let oauthController = JXLOAuthViewController()
        
        let navController = JXLNavigationController(rootViewController: oauthController)
        
        present(navController, animated: true, completion: nil)
    }
    
}
